import cookies from './util.cookies'
import log from './util.log'
import constant from './constant'
import router from '@/router'
import _ from "lodash";

const util = {
    cookies,
    log,
    constant,
    _
}

/**
 * @description 更新标题
 * @param {String} title 标题
 */
util.title = function (titleText) {
    const processTitle = process.env.VUE_APP_TITLE || 'OUTDD'
    window.document.title = `${titleText}${processTitle ? ` | ${processTitle}` : ''}`
}

util.quitSystem = function () {
    //token已被禁止访问
    // 删除cookie
    util.cookies.remove('token')
    util.cookies.remove('uuid')
    // 跳转路由
    router.replace({
        path: '/login',
        query: {
            redirect: router.currentRoute.fullPath//登录之后跳转到对应页面
        }
    });
}

export function don(url, fileName) {
    try {
        const element = document.createElement('a');
        element.href = url;
        element.download = fileName;
        element.style.display = 'none';
        const a = document.body.appendChild(element);
        a.click();

        document.body.removeChild(element);
    } catch (e) {
        console.log(e)
    }
}


/**

 * @param target 目标对象
 * @param source 源对象
 * @param ...exclude  需要排除的字段
 * 对象交集合并-排除
 * 使用样例：
 *  const a:{a:1,b:2,c:9}
 *  const b:{a:2,b:3,c:4,d:5}
 *
 *  objIntersectionAssign(a,b,'a','c')
 *
 *  a == > {b:3}
 */
export function objIntersectionAssignExclude(target = {}, source = {}, ...exclude) {
    const intersection = _.intersection(Object.keys(target), Object.keys(source))

    console.log("objIntersectionAssignExclude")
    objIntersectionAssign(target,source)

    if (exclude) {
        intersection.forEach(it => {
            if(_.find(exclude,(o) =>{  return o == it })){
                console.log("跳过",it)
                delete target[it]
                return;
            }
        })
    }

    return target
}

/**

 * @param target 目标对象
 * @param source 源对象
 * @param ...retain  需要保留的字段
 * 对象交集合并-保留
 * 使用样例：
 *  const a:{a:1,b:2,c:9}
 *  const b:{a:2,b:3,c:4,d:5}
 *
 *  objIntersectionAssign(a,b,'a','c')
 *
 *  a == > {a:2,c:4}
 */
export function objIntersectionAssignRetain(target = {}, source = {}, ...retain) {
    const intersection = _.intersection(Object.keys(target), Object.keys(source))

    objIntersectionAssign(target,source)

    if (retain) {
        intersection.forEach(it => {
            if(_.find(retain,(o) =>{  return o == it })){
                return;
            }
            delete target[it]
        })
    }
}

/**

 * @param target 目标对象
 * @param source 源对象
 * @param ...retain  需要保留的字段
 * 对象交集合并
 * 使用样例：
 *  const a:{a:1,b:2,c:9}
 *  const b:{a:2,b:3,c:4,d:5}
 *
 *  objIntersectionAssign(a,b)
 *
 *  a == > {a:2,b:3,c:4}
 */
export function objIntersectionAssign(target = {}, source = {}) {
    const intersection = _.intersection(Object.keys(target), Object.keys(source))

    let temporary = {}

    intersection.forEach(it => {
        temporary[it] = source[it]
    })
    _.assign(target, temporary);

}


export default util
